package com.base.mine.presenter;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.callback.NavCallback;
import com.alibaba.android.arouter.launcher.ARouter;
import com.base.commonlib.base.BaseApp;
import com.base.commonlib.base.presenter.BasePresenter;
import com.base.commonlib.bean.User;
import com.base.commonlib.http.RealNetWorkUtil;
import com.base.commonlib.http.utils.CallBackImpl;
import com.base.commonlib.sp.SpUtil;
import com.base.commonlib.utils.SystemUtil;
import com.base.commonlib.widget.CommonDialog;
import com.base.mine.R;
import com.base.mine.bean.Version;
import com.base.mine.config.RouteConfig;
import com.base.mine.view.LoginView;


import java.util.HashMap;

import io.reactivex.disposables.Disposable;

/**
 * Created by weikailiang on 2019/9/18.x
 */

public class LoginPresenter extends BasePresenter<LoginView>{
    private int time = 60;
    public String sendPhone = "";
    public LoginPresenter(LoginView view, Activity activity) {
        super(view, activity);
    }

    @Override
    public void start() {
        super.start();
        checkVersion();
    }


    private void checkVersion(){
        mView.showPb_mes("正在检查版本");
        HashMap<String, String> param = new HashMap<>();
        param.put("appVersion", SystemUtil.packageName(mActivity));
        param.put("versionCode", SystemUtil.getVersionCode(mActivity)+"");
        param.put("appName", mActivity.getString(R.string.app_name));
        Disposable disposable = RealNetWorkUtil.postContent()
                .method("api-basic/app/check")
                .content(param)
                .build().enqueue(Version.class, new CallBackImpl<Version>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        mView.hidePb_mes();
                        if (!TextUtils.isEmpty(msg)) {
                            mView.showMes(msg);
                        }
                    }
                    @Override
                    public void onNext(Version version) {
                        mView.hidePb_mes();
                        if (version.isHaveNewVersion()){
                            CommonDialog commonDialog = new CommonDialog(mActivity, "您有新版本：\n"+version.getDescription());
                            commonDialog.setOnPosClick(new CommonDialog.OnPosClick() {
                                @Override
                                public void onClick() {
                                    ARouter.getInstance().build(RouteConfig.ACTIVITY_VERSION).navigation();
                                }
                            });
                            commonDialog.setCancelable(false);
                            commonDialog.show();
                        }
                    }
                });
        mCompositeDisposable.add(disposable);
    }
    public void login(final String mAccount, final String mPassword){

    }

    public void SendCode(final String phone){
        if (phone.length()!=11){
            mView.showMes("请输入正确的手机号");
            return;
        }
        mView.showPb_mes("正在发送短信");
        HashMap<String, String> param = new HashMap<>();
        param.put("phone",phone);
        param.put("code","");
        param.put("platform","1");
        Disposable disposable = RealNetWorkUtil.postContent()
                .method("api-auth/send")
                .content(param)
                .build().enqueue(String.class, new CallBackImpl<String>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        mView.hidePb_mes();
                        if (!TextUtils.isEmpty(msg)) {
                            mView.showMes(msg);
                        }
                    }
                    @Override
                    public void onNext(String version) {
                        mView.hidePb_mes();
                        sendPhone = phone;
                        time = 60;
                        mView.returnSendCodeView().setText(time+"");
                        mView.returnSendCodeView().setTextColor(ContextCompat.getColor(mActivity,R.color.color_tab_text_normal));
                        mHandler.sendEmptyMessageDelayed(SEND_MESSAGE,1000);
                    }
                });
        mCompositeDisposable.add(disposable);
    }
    private static final int SEND_MESSAGE = 1;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case SEND_MESSAGE:
                    if (time==0){
                        mView.returnSendCodeView().setText("发送验证码");
                        mView.returnSendCodeView().setTextColor(ContextCompat.getColor(mActivity,R.color.color_4486FF));
                    }else {
                        time--;
                        mView.returnSendCodeView().setText(time + "");
                        mHandler.sendEmptyMessageDelayed(SEND_MESSAGE, 1000);
                    }
                    break;
            }
        }
    };


    public void phoneLogin(String phone,String code){
        if (phone.length()!=11){
            mView.showMes("请输入正确的手机号");
            return;
        }
        if (TextUtils.isEmpty(code)){
            mView.showMes("请输入验证码");
            return;
        }
        if (code.length()<4){
            mView.showMes("请输入正确验证码");
            return;
        }
        mView.showPb_mes("正在登录。。。");
        HashMap<String, String> param = new HashMap<>();
        param.put("phone",phone);
        param.put("code",code);
        param.put("platform","1");
        Disposable disposable = RealNetWorkUtil.postContent()
                .method("api-auth/autoLogin")
                .content(param)
                .build().enqueue(User.class, new CallBackImpl<User>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        mView.hidePb_mes();
                        if (!TextUtils.isEmpty(msg)) {
                            mView.showMes(msg);
                        }
                    }
                    @Override
                    public void onNext(User user) {
                        mView.hidePb_mes();
                        BaseApp.saveUser(user);
                        SpUtil.setUserAccount(user.getUserName());
//                        SpUtil.setUserPw();
                        SpUtil.setToken(user.getToken());
                        ARouter.getInstance().build("/study/home").navigation(mActivity, new NavCallback() {
                            @Override
                            public void onArrival(Postcard postcard) {
                                mActivity.finish();
                            }
                        });
                    }
                });
        mCompositeDisposable.add(disposable);
    }

    @Override
    public void release() {
        super.release();
        if (mHandler!=null) {
            mHandler.removeCallbacksAndMessages(null);
        }
    }
}
